package com.objecteffects.ilmp.web.utils;

import java.util.List;

import javax.inject.Inject;

import com.objecteffects.ilmp.model.BlogPostsOffsetEntity;
import com.objecteffects.ilmp.model.PostsType;
import com.objecteffects.ilmp.model.TumblrBlogEntity;
import com.objecteffects.ilmp.model.UserEntity;
import com.objecteffects.ilmp.service.UserEntityService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Rusty Wright
 */
public class BlogPostsOffsets {
    private final transient Logger log = LoggerFactory
        .getLogger(BlogPostsOffsets.class);

    private final UserEntityService userEntityService;

    @Inject
    BlogPostsOffsets(final UserEntityService _userEntityService) {
        this.userEntityService = _userEntityService;
    }

    public int findOffset(final String tumblrUrl, final Long userId) {
        final UserEntity userEntity = this.userEntityService.findById(userId);

        final BlogPostsOffsetEntity blogPostsOffsetEntity =
            this.findBlogPostsOffsetEntity(tumblrUrl, userEntity);

        if (blogPostsOffsetEntity == null) {
            this.log.debug("no blogPostsOffsetEntity found");

            return 0;
        }

        this.log
        .debug("found blogPostsOffsetEntity: {}", blogPostsOffsetEntity);

        return blogPostsOffsetEntity.getPostsOffset().intValue();
    }

    public void updateBlogPostsOffsetEntity(final String tumblrUrl,
        final Long userId, final int offset) {

        final UserEntity userEntity = this.userEntityService.findById(userId);

        final BlogPostsOffsetEntity blogPostsOffsetEntity =
            this.findBlogPostsOffsetEntity(tumblrUrl, userEntity);

        if (blogPostsOffsetEntity == null) {
            this.log.debug("blogPostsOffsetEntity is null");

            this.addBlogPostsOffsetEntity(tumblrUrl, userEntity, offset);

            return;
        }

        this.log.debug("updating blogPostsOffsetEntity: {}",
                       Integer.valueOf(offset));

        blogPostsOffsetEntity.setPostsOffset(Integer.valueOf(offset));

        this.userEntityService.update(userEntity);
    }

    /**
     * @param tumblrUrl
     * @param accessToken
     * @param userId
     * @param offset
     */
    private void addBlogPostsOffsetEntity(final String tumblrUrl,
        final UserEntity userEntity, final int offset) {
        final List<TumblrBlogEntity> blogEntities =
            userEntity.getTumblrBlogEntities();

        BlogPostsOffsetEntity newBlogPostsOffsetEntity;

        for (final TumblrBlogEntity each : blogEntities) {
            this.log.debug("comparing: {} with {}", each, tumblrUrl);

            if (each.getUrls().contains(tumblrUrl)) {
                this.log.debug("found TumblrBlogEntity: {}", each);

                newBlogPostsOffsetEntity =
                    new BlogPostsOffsetEntity(Integer.valueOf(offset),
                                              PostsType.POSTS);

                each.getBlogPostsOffsetEntity().add(newBlogPostsOffsetEntity);

                this.userEntityService.update(userEntity);

                break;
            }
        }
    }

    /**
     * @param tumblrUrl
     * @param userId
     * @return null if the offset entity isn't found, otherwise return
     *         the found one.
     */
    @SuppressWarnings("static-method")
    private BlogPostsOffsetEntity findBlogPostsOffsetEntity(
        final String tumblrUrl, final UserEntity userEntity) {
        final List<TumblrBlogEntity> blogEntities =
            userEntity.getTumblrBlogEntities();

        for (final TumblrBlogEntity each : blogEntities) {
            if (each.getUrls().contains(tumblrUrl)) {
                if (each.getBlogPostsOffsetEntity().isEmpty()) {
                    continue;
                }

                return each.getBlogPostsOffsetEntity().get(0);
            }
        }

        return null;
    }
}
